package com.apes.fn.fico.debitnote.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.fico.debitnote.model.BusinessDebitNote;
import com.apes.fn.fico.debitnote.model.LoanNote;
import com.apes.fn.fico.debitnote.model.LoanNoteItem;
import com.apes.fn.fico.debitnote.repository.BusinessDebitNoteRepository;
import com.apes.fn.fico.debitnote.repository.LoanNoteRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.JpaUtil;
import com.apes.framework.util.ReflectUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.fico.debitnote.service
 * @ClassName: LoanNoteService
 * @Author: pyb
 * @Description: 放款单服务
 * @CreateTime: 2021-01-14 10:28
 * @Version: 1.0
 */
@Service("loanNoteService")
public class LoanNoteService extends DomainService {
    @Autowired
    LoanNoteRepository loanNoteRepository;

    @Autowired
    BusinessDebitNoteRepository businessDebitNoteRepository;

    /**
     * 保存
     *
     * @param request
     * @return
     */
    public LoanNote save(SimpleRequest request) {
        LoanNote loanNote = request.getO(LoanNote.class);
        loanNote.setState("create");
        return loanNoteRepository.saveAndFlush(loanNote);
    }

    /**
     * 编辑
     *
     * @param request
     * @return
     */
    public LoanNote update(SimpleRequest request) {
        LoanNote loanNote = request.getO(LoanNote.class);
        return loanNoteRepository.saveAndFlush(loanNote);
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        LoanNote loanNote = request.getO(LoanNote.class);
        loanNoteRepository.delete(loanNote);
        return "删除成功";
    }

    /**
     * 审核
     *
     * @param request
     * @return
     */
    public LoanNote approve(SimpleRequest request) {
        LoanNote loanNote = request.getO(LoanNote.class);
        loanNote.setState("approve");
        loanNote.setApproveUid(getPerson(request));
        loanNote.setApproveDate(new Date());
        loanNote.getLoanNoteItemList().forEach(loanNoteItem -> {
            //获取实体的领域类
            String domainTypeName = JpaUtil.getAllDomainType().stream()
                    .filter(s -> ReflectUtil.getClass(s).getSimpleName().equalsIgnoreCase(loanNoteItem.getOriginBusinessType()))
                    .findFirst().orElse(null);
            CustomRepository repository = JpaUtil.getRepository(domainTypeName);
            Serializable id = JpaUtil.getId(domainTypeName, loanNoteItem.getOrigin());
            Object obj =repository.findOne(id);

            if (obj instanceof BusinessDebitNote) {
                BusinessDebitNote businessDebitNote = (BusinessDebitNote) obj;
                businessDebitNote.setLoan(true);
                businessDebitNote.setLoanDate(new Date());
                businessDebitNote.setLoanUid(getPerson(request));
                businessDebitNoteRepository.saveAndFlush(businessDebitNote);
            }
        });
        return loanNoteRepository.saveAndFlush(loanNote);
    }

    /**
     * 生成放款单
     *
     * @param request
     * @return
     */
    public LoanNote generateLoanNote(SimpleRequest request) {
        JSONObject requestJO = request.getJO();
        String fnCustomerId = requestJO.getString("fnCustomerId");
        //获取
        List<BusinessDebitNote> businessDebitNoteList = businessDebitNoteRepository.findAllByFnCustomerIdAndState(fnCustomerId, "approve");
        PartyRoleRepository roleRepository = SpringManager.getBean(PartyRoleRepository.class);
        PartyRole fnCustomer = roleRepository.findOne(fnCustomerId);
        PartyRole partyRole = roleRepository.findOne("1007");

        LoanNote loanNote = new LoanNote();
        loanNote.setCompany(partyRole);
        loanNote.setFnCustomer(fnCustomer);

        List<LoanNoteItem> loanNoteItems = businessDebitNoteList.stream().map(businessDebitNote -> {
            LoanNoteItem loanNoteItem = new LoanNoteItem();
            loanNoteItem.setAmount(businessDebitNote.getPriceDebit());
            loanNoteItem.setOrigin(businessDebitNote.getInnerBusinessKey());
            loanNoteItem.setOriginBusinessType(businessDebitNote.getInnerBusinessType());
            return loanNoteItem;
        }).collect(Collectors.toList());
        loanNote.getLoanNoteItemList().addAll(loanNoteItems);

        return loanNote;
    }

}
